package yannan.voiplock4ble.db;

import org.greenrobot.greendao.annotation.NotNull;

import java.util.ArrayList;
import java.util.List;

import yannan.voiplock4ble.bean.LocalDBTime;
import yannan.voiplock4ble.bean.LocalDBTimeDao;
import yannan.voiplock4ble.bean.LockBean;
import yannan.voiplock4ble.bean.LockBeanDao;
import yannan.voiplock4ble.bean.LockKeyBean;
import yannan.voiplock4ble.bean.LockKeyBeanDao;
import yannan.voiplock4ble.bean.LockKeyTemp;
import yannan.voiplock4ble.bean.LockRecordBean;
import yannan.voiplock4ble.bean.LockRecordBeanDao;
import yannan.voiplock4ble.bean.UserBean;
import yannan.voiplock4ble.db.daomanager.LocalTimeDaoManager;
import yannan.voiplock4ble.db.daomanager.LockBeanDaoManager;
import yannan.voiplock4ble.db.daomanager.LockKeyDaoManager;
import yannan.voiplock4ble.db.daomanager.LockKeyTempDaoManager;
import yannan.voiplock4ble.db.daomanager.LockRecordDaoManager;
import yannan.voiplock4ble.db.daomanager.UserBeanDaoManager;

/**
 * 说明：
 * 作者：mainTel
 * 时间：2016/12/7 16:10
 * 备注：
 */
public class MyDBHelper {


    /**
     * 根据锁序列号查找门锁
     *
     * @param lockserialId
     * @return
     */
    public static LockBean getLockBean(@NotNull String lockserialId) {
        try {
            List<LockBean> list = DaoUtils.getDao(LockBeanDaoManager.class).getQueryBuilder()
                    .where(LockBeanDao.Properties.LockserialId.eq(lockserialId))
                    .build().list();
            LockBean bean = list.get(0);
            if (bean != null)
                return bean;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取本地锁列表
     *
     * @return
     */
    public static List<LockBean> getLockBeanList() {
        try {
            List<LockBean> list = DaoUtils.getDao(LockBeanDaoManager.class).getQueryBuilder()
                    .build().list();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取本地账号
     *
     * @return
     */
    public static UserBean getUserBean() {
        try {
            List<UserBean> userBeans = DaoUtils.getDao(UserBeanDaoManager.class).getQueryBuilder()
                    .build().list();
            UserBean bean = userBeans.get(0);
            if (bean != null) {
                return bean;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 插入一条门锁数据
     *
     * @param lockBean
     * @return
     */
    public static boolean insertLockBean(@NotNull LockBean lockBean) {
        try {
            return DaoUtils.getDao(LockBeanDaoManager.class).insertOrReplace(lockBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据序列号删除门锁
     *
     * @param lockserialId
     * @return
     */
    public static boolean delLockBean(@NotNull String lockserialId) {

        try {
            List<LockBean> list = DaoUtils.getDao(LockBeanDaoManager.class).getQueryBuilder()
                    .where(LockBeanDao.Properties.LockserialId.eq(lockserialId))
                    .build().list();
            LockBean bean = list.get(0);
            return DaoUtils.getDao(LockBeanDaoManager.class).delete(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 根据序列号获取钥匙列表
     *
     * @param lockserialId
     * @param type
     * @return
     */
    public static List<LockKeyBean> getLockKeyBeanListByID(@NotNull String lockserialId, String type) {
        try {
            List<LockKeyBean> list = DaoUtils.getDao(LockKeyDaoManager.class).getQueryBuilder()
                    .where(LockKeyBeanDao.Properties.LockserialId.eq(lockserialId)
                            , LockKeyBeanDao.Properties.Type.eq(type)
                    ).orderDesc(LockKeyBeanDao.Properties.RegisterTime).build().list();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 储存所用户
     *
     * @param bean
     * @return
     */
    public static boolean installKeyBean(LockKeyBean bean) {
        try {



            return DaoUtils.getDao(LockKeyDaoManager.class).insertOrReplace(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据身份ID 获取钥匙
     *
     * @param userIdentifier
     * @return
     */
    public static LockKeyBean getLockKeyBeanByID(String lockserialId, String userIdentifier) {
        try {
            LockKeyBean key = DaoUtils.getDao(LockKeyDaoManager.class).getQueryBuilder().where(
                    LockKeyBeanDao.Properties.LockserialId.eq(lockserialId),
                    LockKeyBeanDao.Properties.UserIdentifier.eq(userIdentifier)
            ).build().list().get(0);
            return key;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取开锁记录从本地
     *
     * @param lockserialId
     * @return
     */
    public static List<LockRecordBean> getLockRedordList(String lockserialId) {
        try {
            List<LockRecordBean> list = DaoUtils.getDao(LockRecordDaoManager.class).getQueryBuilder()
                    .where(LockRecordBeanDao.Properties.LockserialId.eq(lockserialId))
                    .orderDesc(LockRecordBeanDao.Properties.UnlockingDate)
                    .list();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 修改本地时间
     *
     * @param lockserialId
     * @param time
     * @param type         0 指静脉时间 1密码时间
     */
    public static void upDateLocalTime(String lockserialId, String time, String type) {
        try {
            List<LocalDBTime> list = DaoUtils.getDao(LocalTimeDaoManager.class).getQueryBuilder()
                    .where(LocalDBTimeDao.Properties.LockserialId.eq(lockserialId))
                    .build().list();
            if (list.size() != 0) {
                LocalDBTime bean = list.get(0);
                if (type.equals("0")) {
                    bean.veinKeyTime = time;
                } else if (type.equals("1")) {
                    bean.pwdKeyTime = time;
                }
                DaoUtils.getDao(LocalTimeDaoManager.class).insertOrReplace(bean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 根据序列号和类型删除所有锁用户
     *
     * @param lockserialId
     * @param type
     */
    public static boolean delLockKeyAll(String lockserialId, String type) {
        try {
            List<LockKeyBean> listDel = DaoUtils.getDao(LockKeyDaoManager.class)
                    .getQueryBuilder().where(LockKeyBeanDao.Properties.LockserialId.eq(lockserialId)
                            , LockKeyBeanDao.Properties.Type.eq(type)).build().list();
            return DaoUtils.getDao(LockKeyDaoManager.class).deleteList(listDel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 拷贝数据到临时表
     *
     * @param lockserialId
     * @param type
     */
    public static boolean copyLockKeyList(String lockserialId, String type) {
        try {
            List<LockKeyBean> listDel = DaoUtils.getDao(LockKeyDaoManager.class)
                    .getQueryBuilder().where(LockKeyBeanDao.Properties.LockserialId.eq(lockserialId)
                            , LockKeyBeanDao.Properties.Type.eq(type)).build().list();
            List<LockKeyTemp> listTemp = new ArrayList<>();
            for (LockKeyBean bean :
                    listDel) {
                LockKeyTemp temp = new LockKeyTemp();
                temp.userIdentifier = bean.userIdentifier;
                temp.nickName = bean.nickName;
                temp.userPermission = bean.userPermission;
                temp.isBan = bean.isBan;
                temp.type = bean.type;
                temp.lockserialId = bean.lockserialId;
                temp.userId = bean.userId;
                temp.registerTime = bean.registerTime;
                temp.templateId = bean.templateId;
                listTemp.add(temp);
            }
            DaoUtils.getDao(LockKeyTempDaoManager.class).deleteAll();
            if (listTemp.size() == 0) {
                return true;
            } else {
                return DaoUtils.getDao(LockKeyTempDaoManager.class).insertOrReplaceList(listTemp);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
